package com.bci.pwtz.service.impl;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.service.BusinessCommonService;

public class BusinessCommonServiceImpl implements BusinessCommonService
{
    private static final Logger _logger=Logger.getLogger(BusinessCommonServiceImpl.class);    
    public static Map<Long,String>  TABLE_NAME_MAP = new HashMap<Long, String>();
    private JdbcTemplate jdbcTemplate;    
    
    static 
    {
        TABLE_NAME_MAP.put(1L, "T_PDBYQ");
        TABLE_NAME_MAP.put(2L, "T_ZSBYQ");
        TABLE_NAME_MAP.put(3L, "T_DYXL");
        TABLE_NAME_MAP.put(4L, "T_DYGT");
        TABLE_NAME_MAP.put(5L, "T_DYDX");
        TABLE_NAME_MAP.put(6L, "T_DYZSKG");
        TABLE_NAME_MAP.put(7L, "T_DYZRDQ");
        TABLE_NAME_MAP.put(8L, "T_DYRSX");
        TABLE_NAME_MAP.put(10L, "T_DYZSDRQ");
        TABLE_NAME_MAP.put(11L, "T_DYWGBCZZ");
        TABLE_NAME_MAP.put(12L, "T_DYZSBLQ");
        TABLE_NAME_MAP.put(13L, "T_DYDL");
        TABLE_NAME_MAP.put(14L, "T_DYDLZD");
        TABLE_NAME_MAP.put(16L, "T_DYPDX");
        TABLE_NAME_MAP.put(17L, "T_DYDLFJX");
        TABLE_NAME_MAP.put(18L, "T_DYKG");
        TABLE_NAME_MAP.put(19L, "T_DYMX");
        TABLE_NAME_MAP.put(20L, "T_DYPDP");
        TABLE_NAME_MAP.put(21L, "T_DYKGG");
        TABLE_NAME_MAP.put(22L, "T_FGDZZ");
        TABLE_NAME_MAP.put(23L, "T_DYSYDLBHQ");
        TABLE_NAME_MAP.put(24L, "T_DYBX");
        TABLE_NAME_MAP.put(25L, "T_DYJCKY");
        TABLE_NAME_MAP.put(26L, "T_DYYHJRD");
        
        TABLE_NAME_MAP.put(100L, "T_BDZ");  
        TABLE_NAME_MAP.put(101L, "T_ZYXL");  
        TABLE_NAME_MAP.put(102L, "T_ZYGT");   
        TABLE_NAME_MAP.put(103L, "T_ZYZSGBFJB");   
        TABLE_NAME_MAP.put(104L, "T_ZYZSDLQ"); 
        TABLE_NAME_MAP.put(105L, "T_ZYZSFHKG"); 
        TABLE_NAME_MAP.put(106L, "T_ZYZSGLKG");  
        TABLE_NAME_MAP.put(107L, "T_ZYZSCHQ");
        TABLE_NAME_MAP.put(108L, "T_ZYZSDLSRDQ");
        TABLE_NAME_MAP.put(109L, "T_ZYZSBLQ");
        TABLE_NAME_MAP.put(110L, "T_ZYZSGZZSQ");   
        TABLE_NAME_MAP.put(111L, "T_ZYZSDRQ"); 
        TABLE_NAME_MAP.put(112L, "T_ZYZSDYHGQ");  
        TABLE_NAME_MAP.put(113L, "T_ZYZSDLHGQ");
        TABLE_NAME_MAP.put(114L, "T_ZYZSZHHGQ");   
        TABLE_NAME_MAP.put(115L, "T_ZYZSFDQ");   
        TABLE_NAME_MAP.put(116L, "T_ZYZSTRBYQ");  
        TABLE_NAME_MAP.put(117L, "T_ZYZSZDTYQ");  
        TABLE_NAME_MAP.put(118L, "T_ZYZSFSSB");  
        TABLE_NAME_MAP.put(119L, "T_ZYDX");
        TABLE_NAME_MAP.put(120L, "T_ZYDL");   
        TABLE_NAME_MAP.put(121L, "T_ZYDLZD"); 
        TABLE_NAME_MAP.put(122L, "T_ZYDLJT");
        TABLE_NAME_MAP.put(123L, "T_ZYJCKY");   
        TABLE_NAME_MAP.put(124L, "T_ZYKGZ");   
        TABLE_NAME_MAP.put(125L, "T_ZYPDS"); 
        TABLE_NAME_MAP.put(126L, "T_ZYHWG"); 
        TABLE_NAME_MAP.put(127L, "T_ZYXSBDZ");  
        TABLE_NAME_MAP.put(128L, "T_ZYDLFZX");
        TABLE_NAME_MAP.put(129L, "T_ZYZNGDY");
        TABLE_NAME_MAP.put(130L, "T_ZYZNSYB");
        TABLE_NAME_MAP.put(131L, "T_ZYZNDLQ");   
        TABLE_NAME_MAP.put(132L, "T_ZYZNGLKG"); 
        TABLE_NAME_MAP.put(133L, "T_ZYZNFHKG");  
        TABLE_NAME_MAP.put(134L, "T_ZYZNRDQ");
        TABLE_NAME_MAP.put(135L, "T_ZYZNMX");   
        TABLE_NAME_MAP.put(136L, "T_ZYZNDKQ");   
        TABLE_NAME_MAP.put(137L, "T_ZYZNDLHGQ");  
        TABLE_NAME_MAP.put(138L, "T_ZYZNDYHGQ");  
        TABLE_NAME_MAP.put(139L, "T_ZYZNZHHGQ");  
        TABLE_NAME_MAP.put(140L, "T_ZYZNDLDRQ");
        TABLE_NAME_MAP.put(141L, "T_ZYZNBLQ");   
        TABLE_NAME_MAP.put(142L, "T_ZYZNKGG"); 
        TABLE_NAME_MAP.put(143L, "T_ZYZNGZZSQ");
        TABLE_NAME_MAP.put(144L, "T_ZYDLJ");

        
    }

    @Override
    public String getDeviceName(Long deviceTypeId, String deviceId) throws PwtzDatabaseException
    {
        if(deviceTypeId==null || deviceTypeId == 0 || deviceId == null || deviceId.length() == 0)
            return "";
        final String sql = "SELECT DEVICENAME FROM {0} WHERE DEVICEID=? AND STATUS=1";
        if(!TABLE_NAME_MAP.containsKey(deviceTypeId))
            return "";
        String tableName = TABLE_NAME_MAP.get(deviceTypeId);
        try
        {
            List<Map<String,Object>> objs =jdbcTemplate.queryForList(MessageFormat.format(sql, tableName),deviceId);
            if(objs == null || objs.size() == 0)
                return "";
            return objs.get(0).get("DEVICENAME").toString();
        }
        catch(DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
    }
    
    @Override
    public int getCountByDeviceName(Long companyId,Long deviceTypeId, String deviceName) throws PwtzDatabaseException
    {
        if(deviceTypeId==null || deviceTypeId == 0 || deviceName == null || "".equals(deviceName))
            return 0;
        final String sql = "SELECT COUNT(1) FROM {0} WHERE CompanyID = ? AND  DEVICENAME = ? AND STATUS=1";
        if(!TABLE_NAME_MAP.containsKey(deviceTypeId))
            return 0;
        String tableName = TABLE_NAME_MAP.get(deviceTypeId);
        try
        {
            int ret =jdbcTemplate.queryForInt(MessageFormat.format(sql, tableName),companyId,deviceName);
            return ret;
        }
        catch(DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
    }
    
    @Override
    public int getCountByDeviceName(Long companyId,Long deviceTypeId, String deviceName,String deviceId) throws PwtzDatabaseException
    {
        if(deviceTypeId==null || deviceTypeId == 0 || deviceName == null || "".equals(deviceName))
            return 0;
        final String sql = "SELECT COUNT(1) FROM {0} WHERE CompanyID = ? AND DEVICENAME = ? AND DEVICEID != ? AND STATUS=1";
        if(!TABLE_NAME_MAP.containsKey(deviceTypeId))
            return 0;
        String tableName = TABLE_NAME_MAP.get(deviceTypeId);
        try
        {
            int ret =jdbcTemplate.queryForInt(MessageFormat.format(sql, tableName),companyId,deviceName,deviceId);
            return ret;
        }
        catch(DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
    }
    
    public JdbcTemplate getJdbcTemplate()
    {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
    {
        this.jdbcTemplate = jdbcTemplate;
    }

}
